package controllers

import (
	"encoding/json"
	"supershare/backend/models"
	"supershare/backend/utils"

	"github.com/beego/beego/v2/server/web"
)

// CleaningSettingController 保洁设置控制器
type CleaningSettingController struct {
	web.Controller
}

// List 获取保洁设置列表
// @Title List
// @Description get cleaning settings
// @Param page query int false "Page number"
// @Param page_size query int false "Page size"
// @Param store_id query int false "Store ID"
// @Success 200 {object} utils.PagedResponse
// @router / [get]
func (c *CleaningSettingController) List() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}
	page, _ := c.GetInt("page", 1)
	pageSize, _ := c.GetInt("page_size", 10)
	storeId, _ := c.GetInt64("store_id")

	// 解析过滤条件
	filters := make(map[string]interface{})

	// 加盟商管理员只能查看自己加盟商的数据
	if permResult.Admin.FranchiseId > 0 {
		filters["franchise_id"] = permResult.Admin.FranchiseId
	}
	// 门店管理员只能查看自己门店的数据
	if storeId > 0 {
		filters["store_id"] = storeId
	}

	settings, total, err := models.GetList[models.CleaningSetting](page, pageSize, filters)
	if err != nil {
		c.Data["json"] = FailureResponse(err)
	} else {
		c.Data["json"] = SuccessResponse(utils.NewPagedResponse(settings, page, pageSize, total))
	}
	c.ServeJSON()
}

// Get 获取单个保洁设置
// @Title Get
// @Description get cleaning setting by id
// @Param id path int true "Setting ID"
// @Success 200 {object} models.CleaningSetting
// @router /:id [get]
func (c *CleaningSettingController) Get() {
	id, _ := c.GetInt64(":id")
	setting, err := models.GetCleaningSettingById(id)
	if err != nil {
		c.Data["json"] = FailureResponse(err)
	} else {
		c.Data["json"] = SuccessResponse(setting)
	}
	c.ServeJSON()
}

// Create 新建保洁设置
// @Title Create
// @Description create cleaning setting
// @Param body body models.CleaningSetting true "body for cleaning setting content"
// @Success 201 {object} models.CleaningSetting
// @Failure 403 body is empty
// @router / [post]
func (c *CleaningSettingController) Create() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}
	var setting models.CleaningSetting
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &setting); err != nil {
		c.Data["json"] = FailureResponse(err)
		c.ServeJSON()
		return
	}
	setting.FranchiseId = permResult.Admin.FranchiseId
	newSetting, err := models.CreateOrUpdateCleaningSetting(&setting)
	if err != nil {
		c.Data["json"] = FailureResponse(err)
	} else {
		c.Data["json"] = SuccessResponse(newSetting)
	}
	c.ServeJSON()
}

// Update 更新保洁设置
// @Title Update
// @Description update the cleaning setting
// @Param id path int true "The id you want to update"
// @Param body body models.CleaningSetting true "body for cleaning setting content"
// @Success 200 {object} models.CleaningSetting
// @Failure 403 :id is not int
// @router /:id [put]
func (c *CleaningSettingController) Update() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}

	id, _ := c.GetInt64(":id")
	var setting models.CleaningSetting
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &setting); err != nil {
		c.Data["json"] = FailureResponse(err)
		c.ServeJSON()
		return
	}
	setting.Id = id
	updatedSetting, err := models.CreateOrUpdateCleaningSetting(&setting)
	if err != nil {
		c.Data["json"] = FailureResponse(err)
	} else {
		c.Data["json"] = SuccessResponse(updatedSetting)
	}
	c.ServeJSON()
}

// Delete 删除保洁设置
// @Title Delete
// @Description delete the cleaning setting
// @Param id path int true "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *CleaningSettingController) Delete() {
	id, _ := c.GetInt64(":id")
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}
	// 检查是否存在
	setting, err := models.GetCleaningSettingById(id)
	if err != nil {
		c.Data["json"] = FailureResponse(err)
		c.ServeJSON()
		return
	}
	// 检查权限
	if setting.FranchiseId != permResult.Admin.FranchiseId {
		c.Data["json"] = ErrorResponse("Permission denied")
		c.ServeJSON()
		return
	}
	if err := models.DeleteCleaningSetting(id); err != nil {
		c.Data["json"] = FailureResponse(err)
	} else {
		c.Data["json"] = SuccessResponse("Deleted successfully")
	}
	c.ServeJSON()
}
